<template>
	<view class="login login-bg">
		<cu-custom bgColor="bg-blue2" :isBack="false"><block slot="backText">返回</block><block slot="content">登录</block></cu-custom>
		<view class="content">
			<!-- 头部logo -->
			<view class="header">
				<image :src="logoImage"></image>
			</view>
			<!-- 主体表单 -->
			<view class="main">
				<wInput v-model="userName" type="text" maxlength="20" placeholder="用户名"></wInput>
				<wInput v-model="password" type="password" maxlength="20" placeholder="密码" isShowPass></wInput>
			</view>
			<wButton text="登 录" :rotate="isRotate" @click.native="startLogin()"></wButton>
		</view>
	</view>
</template>

<script>
	import wInput from '@/components/watch-login/watch-input.vue'
	import wButton from '@/components/watch-login/watch-button.vue'
	import md5 from '@/common/lib/md5.min.js'

	export default {
		data() {
			return {
				// logo图片
				logoImage: '/static/img/logo.png',
				isRotate: false, //是否加载旋转
				userName: 'admin',
				password: 'Aa000000',
				clientId: 'console-client-demo',
				clientSecret: '1q2w3e*',
				grantType: 'password',
				tenant: 'ca312222-ff35-27d6-b66d-3a0f98585dbf'
			};
		},
		components: {
			wInput,
			wButton,
		},
		onLoad(options) {
			if (options.errorMsg) {
				uni.showToast({
					title: options.errorMsg,
					icon: 'none'
				})
			}
		},
		methods: {
			login(params) {
				if (params) {
					this.isRotate = true
					this.$store.dispatch('login', params).then(res => {
						this.$store.dispatch('getInfo').then(res => {
							this.isRotate = false
							uni.reLaunch({
								url: '/pages/main/index',
							})
						}).catch((res) => {
							this.isRotate = false
							console.log(res)
						})
						// this.isRotate = false
						// uni.reLaunch({
						// 	url: '/pages/main/index',
						// })
					}).catch((res) => {
						console.log(res)
						let message = '登录失败'
						if (res && res.data) {
							message = res.data.message
						}
						uni.showToast({
							icon: 'none',
						    title: message,
						    duration: 2000
						})
						this.isRotate = false
					})
				}
			},
			startLogin() {
				//登录
				if (this.isRotate) {
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.userName.length == "") {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '用户名不能为空'
					});
					return;
				}
				if (this.password.length == "") {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '密码不能为空'
					});
					return;
				}

				// 网络请求
				const params = {
					username: this.userName,
					password: this.password,
					client_id: this.clientId,
					client_secret: this.clientSecret,
					grant_type: this.grantType,
					__tenant: this.tenant
				}
				this.login(params)
			},
			bindPickerChange: function(e) {
				this.index = e.target.value
			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("./css/main.css");

	.login-bg {
		/* width: 100%;
		height: 100%;
		background-image: url('/static/img/login-bg.png');
		background-size: cover;
		background-position: 50%;
		position: absolute; */
	}
	
	.header {
		margin-top: 0 !important;
		margin-bottom: 0 !important;
	}

	.login-bg .content .header image {
		width: 284rpx;
		height: 284rpx;
	}

	/* .login-bg .content .header {
		width: 241rpx;
		height: 241rpx;
		background-size: 241rpx;
		background-image: url('/static/img/logo.png');
	} */
	
	.main-list{
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		height: 36upx;   /* Input 高度 */
		color: #333333;
		padding: 32upx;
	/* 	margin-top:24upx;
		margin-bottom: 24upx; */
	}
	.img{
		width: 53upx;
		height: 53upx;
		font-size: 32upx;
	}
	.main-input{
		flex: 1;
		text-align: left;
		font-size: 28upx;
		/* line-height: 100upx; */
		padding-right: 10upx;
		margin-left: 20upx;
		width: 400upx;
	}
	.vercode {
		color: rgba(0,0,0,0.7);
		font-size: 24upx;
		line-height: 100upx;
	}
	.vercode-run {
		color: rgba(0,0,0,0.4) !important;
	}
	.oBorder{
	    border: none;
	    border-radius: 2.5rem ;
	    -webkit-box-shadow: 0 0 60upx 0 rgba(43,86,112,.1) ;
	    box-shadow: 0 0 60upx 0 rgba(43,86,112,.1) ;
	}
</style>
